package day13.Homework;

import java.io.*;
import java.util.*;

//d.实现注册功能 要求:
//选择卡号（提供以“139”开头的9个随机卡号,每行3个）
//选择套餐（话唠套餐、网虫套餐、超人套餐） 输入用户名、密码信息
//输入预存话费金额
//成功保存信息并提示 显示(卡号、用户名、当前余额、套餐信息)
public class PhonePlanSystem {
    static Map<String, User> map = new HashMap<>();
    static Random r = new Random();
    static Scanner sc = new Scanner(System.in);

    static {    //反序列化
        input();
    }

    public static void main(String[] args) {
        menu1();
    }

    public static void menu1() {
        while (true) {
            System.out.println("XXX用户系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入编号:");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    login();
                    break;
                case 2:
                    reg();
                    break;
                case 3:
                    return;
            }
        }
    }

    public static void login() {
        System.out.println();
        for (String key : map.keySet()) {
            User value = map.get(key);
            value.print();
        }
    }

    public static void reg() {
        System.out.println("请输入你喜欢的电话号码");
        String[] phones = randomPhone();
        for (int i = 0; i < phones.length; i++) {
            System.out.print(i + 1 + "." + phones[i] + " ");
            if ((i + 1) % 3 == 0) {
                System.out.println();
            }
        }

        System.out.println("请输入编号1-9");
        int n = sc.nextInt();
        String phone = phones[n - 1];
        System.out.println("请选择套餐:");
        System.out.println("1.话唠套餐 " + " 2.超人套餐 " + " 3.网虫套餐");
        int m = sc.nextInt();
        Plan p = null;
        switch (m) {
            case 1:
                p = new Chatterbox(19.9);
                break;
            case 2:
                p = new Superman(29.9);
                break;
            case 3:
                p = new Networm(9.9);
                break;
        }
        Plan plan = p;
        System.out.println("请输入用户名");
        String name = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        System.out.println("请输入你预存的话费");
        double money = 0;
        while (true) {
            money = sc.nextDouble();
            if (money < p.price) {
                System.out.println("你的花费不足以支付这个套餐,请重新充值");
            } else {
                System.out.println("套餐购买成功");
                break;
            }
        }
        User user = new User(phone, name, password, p, p.price, money - p.price);
        user.print();
        map.put(phone, user);
        //序列化
        Output();
    }

    public static String[] randomPhone() {
        String[] phones = new String[9];
        Set<String> s = new HashSet<>();
//        while (s.size()!=9){
//
//        }
        for (int i = 0; i < phones.length; i++) {
            StringBuilder sb = new StringBuilder("139");
            for (int j = 0; j < 8; j++) {
                sb.append(r.nextInt(10));
            }
            phones[i] = sb.toString();
        }
        return phones;
    }

    public static void input() { //反序列话
        try {
            FileInputStream fis = new FileInputStream("d://a//PhonePlanSystem.xxx");
            ObjectInputStream ois = new ObjectInputStream(fis);
            map = (Map<String, User>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("欢迎第一次使用!");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void Output() {    //序列化
        File f = new File("d://a//PhonePlanSystem.xxx");
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            System.out.println("用户信息保存成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void randomPhoneNum() {
        ArrayList<String> phones = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            int phoneNum = r.nextInt(90000000) + 10000000;
            String phone = "139" + phoneNum;
            phones.add(phone);
        }
        for (int i = 0; i < phones.size(); i++) {
            if (i == 3 || i == 6) {
                System.out.println();
            }
            System.out.print(phones.get(i) + " ");
        }
        System.out.println();
    }
}